package com.csun.dsms.task;

import cn.hutool.core.util.StrUtil;
import com.csun.dsms.mapper.HgFactoryPredictionDataMapper;
import com.csun.dsms.mapper.HgIndustryElectricityDataMapper;
import com.csun.dsms.mapper.HgUserMapper;
import com.csun.dsms.model.huang.*;
import com.csun.dsms.service.*;
import com.csun.dsms.utils.pt.HuangGangUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
public class PeriodQuartz extends QuartzJobBean {

    @Resource
    private HgReminderDataService hgReminderDataService;
    @Resource
    private HgGdpDataService hgGdpDataService;
    @Resource
    private HgFixedAssetDataService hgFixedAssetDataService;
    @Resource
    private HgFactoryDataService hgFactoryDataService;
    @Resource
    private HgConsumptionDataService hgConsumptionDataService;
    @Resource
    private HgInvestmentDataService hgInvestmentDataService;
    @Resource
    private HgFinancialDataService hgFinancialDataService;
    @Resource
    private HgInstructionDataService hgInstructionDataService;
    @Resource
    private HgMarketDataService hgMarketDataService;
    @Resource
    private HgUserMapper hgUserMapper;
    @Resource
    private HgIndustryElectricityDataMapper hgIndustryElectricityDataMapper;
    @Resource
    private HgFactoryPredictionDataMapper hgFactoryPredictionDataMapper;
    @Resource
    private HgInvestmentPartDataService hgInvestmentPartDataService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDataMap jobMap = jobExecutionContext.getJobDetail().getJobDataMap();
        // HgPushCycleData
//        HgPushCycleData hgPushCycleData = new HgPushCycleData();
        String ihgId = jobMap.getString("ihgId");
        String phone = jobMap.getString("phone");
        String userName = jobMap.getString("userName");
        String department = jobMap.getString("department");
        String remark = jobMap.getString("remark");
        String title = jobMap.getString("title");
        int catalog = jobMap.getInt("catalog");
        int type = jobMap.getInt("type");
        log.info("executeInternal=== >>iHgId = "+ihgId);
        HgReminderData hgReminderData = new HgReminderData();
        hgReminderData.setIHgUser(ihgId);
        hgReminderData.setReminderPerson(userName);
        hgReminderData.setCreatedTime(new Date());
        hgReminderData.setReminderDepart(department);
        hgReminderData.setReminderPhoneNumber(phone);
        hgReminderData.setReminderTime(new Date());
        String catalogName = getNameByCatalog(catalog);
        //根据type 区分类型
        //1. 提前预警
        //2. 上传完成后通知查看 知道数据通知给谁
        // 根据department 来查找数据
        // 3. 数据没上传的再次提醒去上传;
        // 4..排名靠后的通知
        if (type == 1){
            // 推送数据
            hgReminderData.setReminderType(1);
            if (StringUtils.isEmpty(remark)){
                hgReminderData.setReminderContent("提醒"+catalogName+"数据及时上报");
            }else{
                hgReminderData.setReminderContent(remark);
            }
            if (StringUtils.isEmpty(title)){
                hgReminderData.setReminderTitle(catalogName+"数据上报提醒");
            }else{
                hgReminderData.setReminderTitle(title);
            }
            hgReminderDataService.save(hgReminderData);
            HuangGangUtil.sendMsg(hgReminderData);
        }else if (type == 2){
            // 查找最新的数据
            boolean isNewUpload = checkIsUpload(catalog);
            if (isNewUpload){
                hgReminderData.setReminderType(2);
                if (StringUtils.isEmpty(remark)){
                    hgReminderData.setReminderContent(catalogName+"数据上报完毕,点击查看");
                }else{
                    hgReminderData.setReminderContent(remark);
                }
                if (StringUtils.isEmpty(title)){
                    hgReminderData.setReminderTitle("最新数据已上传");
                }else{
                    hgReminderData.setReminderTitle(title);
                }
                hgReminderDataService.save(hgReminderData);
            }
        }else if (type == 3){
            // 数据没上传的再次提醒去上传
            boolean isNewUpload = checkIsUpload(catalog);
            if (!isNewUpload){
                hgReminderData.setReminderType(3);
                if (StringUtils.isEmpty(remark)){
                    hgReminderData.setReminderContent(catalogName+"数据超时未上报,请及时上传");
                }else{
                    hgReminderData.setReminderContent(remark);
                }
                if (StringUtils.isEmpty(title)){
                    hgReminderData.setReminderTitle(catalogName+"数据超时未上报");
                }else{
                    hgReminderData.setReminderTitle(title);
                }
                hgReminderDataService.save(hgReminderData);
                HuangGangUtil.sendMsg(hgReminderData);
            }
        }else if (type ==4){
            // 增速排名靠后的推送到分管领导（最后三名）
            checkIsUploadByGrowthRateLast3(catalog,catalogName);
        }else if (type ==5){
            // 用电量最少三名
            checkIsUploadByPowerCount3(catalog,catalogName);
        }else if (type ==6){
            //工业产值增长最后3位
            checkIsUploadByFactoryGrowthRate3(catalog,catalogName);
        }else if (type ==7){
            //专班招商最后3位
            checkIsUploadByInvestmentLast3(catalog,catalogName);
        }
    }


    private String getNameByCatalog(int catalog){
        String name = "";
        switch (catalog){
            case 1:
                name = "地区生产总值";
                break;
            case 2:
                name = "固定资产投资";
                break;
            case 3:
                name = "规上工业";
                break;
            case 4:
                name = "社会消费品零售总额";
                break;
            case 5:
                name = "招商引资";
                break;
            case 6:
                name = "争资争项";
                break;
            case 7:
                name = "项目建设";
                break;
            case 8:
                name = "市场主体培育";
                break;
        }
        return name;
    }


    private boolean checkIsUpload(int catalog) {
        if (catalog == 0){
            return false;
        }
        HgQueryDto hgQueryDto = new HgQueryDto();
        hgQueryDto.setPageSize(1);
        boolean isCurrentMonth = false;
        switch (catalog){
            case 1:
                List<HgGdpData> newGdpData =  hgGdpDataService.queryGdpDataList(hgQueryDto);
                if (newGdpData!=null && newGdpData.size()>0){
                    isCurrentMonth = isDateInCurrentMonth(newGdpData.get(0).getCreatedTime());
                }
                break;
            case 2:
                List<HgFixedAssetData> newFixedData = hgFixedAssetDataService.queryDataList(hgQueryDto);
                if (newFixedData!=null && newFixedData.size()>0){
                    isCurrentMonth = isDateInCurrentMonth(newFixedData.get(0).getCreatedTime());
                }
                break;
            case 3:
                List<HgFactoryData> newFactoryList = hgFactoryDataService.queryDataList(hgQueryDto);
                if (newFactoryList!=null && newFactoryList.size()>0){
                    isCurrentMonth = isDateInCurrentMonth(newFactoryList.get(0).getCreatedTime());
                }
                break;
            case 4:
                List<HgConsumptionData> hgConsumptionDataList = hgConsumptionDataService.queryDataList(hgQueryDto);
                if (hgConsumptionDataList!=null && hgConsumptionDataList.size()>0){
                    isCurrentMonth = isDateInCurrentMonth(hgConsumptionDataList.get(0).getCreatedTime());
                }
                break;
            case 5:
                List<HgInvestmentData> hgInvestmentData = hgInvestmentDataService.queryDataList(hgQueryDto);
                if (hgInvestmentData!=null && hgInvestmentData.size()>0){
                    isCurrentMonth = isDateInCurrentMonth(hgInvestmentData.get(0).getCreatedTime());
                }
                break;
            case 6:
                List<HgFinancialData> hgFinancialData = hgFinancialDataService.queryDataList(hgQueryDto);
                if (hgFinancialData!=null && hgFinancialData.size()>0){
                    isCurrentMonth = isDateInCurrentMonth(hgFinancialData.get(0).getCreatedTime());
                }
                break;
            case 7:
                List<HgInstructionListData> hgInstructionListData = hgInstructionDataService.queryInstructionList(hgQueryDto);
                if (hgInstructionListData!=null && hgInstructionListData.size()>0){
                    isCurrentMonth = isDateInCurrentMonth(hgInstructionListData.get(0).getCreatedTime());
                }
                break;
            case 8:
                List<HgMarketData> hgMarketDataList= hgMarketDataService.queryDataList(hgQueryDto);
                if (hgMarketDataList!=null && hgMarketDataList.size()>0){
                    isCurrentMonth = isDateInCurrentMonth(hgMarketDataList.get(0).getCreatedTime());
                }
                break;
        }
        return isCurrentMonth;
    }

    /**
     * 增速最后3名
     */
    private void checkIsUploadByGrowthRateLast3(int catalog, String catalogName) {
        if (catalog == 0) {
            return;
        }
        HgQueryDto hgQueryDto = new HgQueryDto();
        hgQueryDto.setPageSize(1000);

        switch (catalog) {
            case 1:
                hgQueryDto.setAreaLevel(2);
                hgQueryDto.setIndustryLevel(0);
                List<HgGdpData> gdpDataList = hgGdpDataService.queryGdpDataList(hgQueryDto);
                gdpDataList.sort(Comparator.comparingDouble(HgGdpData::getGrowthRate));
                for (int i = 0; i < 3; i++) {
                    HgGdpData hgGdpData = gdpDataList.get(i);
                    String area = hgGdpData.getArea();
                    if(StrUtil.isBlank(area)){
                        continue;
                    }
                    List<HgUser> hgUsers = hgUserMapper.selectUserByArea(area);
                    if (hgUsers != null && !hgUsers.isEmpty()) {
                        HgUser hgUser = hgUsers.get(0);

                        HgReminderData hgReminderDataNew = new HgReminderData();
                        Date date = new Date();
                        hgReminderDataNew.setIHgUser(hgUser.getHuangGangId());
                        hgReminderDataNew.setReminderPerson(hgUser.getRealName());
                        hgReminderDataNew.setCreatedTime(date);
                        hgReminderDataNew.setReminderDepart(hgUser.getDepartment());
                        hgReminderDataNew.setReminderPhoneNumber(hgUser.getPhone());
                        hgReminderDataNew.setReminderTime(date);
                        hgReminderDataNew.setReminderType(4);
                        hgReminderDataNew.setReminderContent(catalogName + "gdp增速垫底，请注意！");
                        hgReminderDataNew.setReminderTitle(catalogName + "gdp增速垫底，请注意！");
                        hgReminderDataService.save(hgReminderDataNew);
                        HuangGangUtil.sendMsg(hgReminderDataNew);
                    }
                }
                break;
            case 2:
                hgQueryDto.setAreaLevel(2);
                List<HgFixedAssetData> fixedDataList = hgFixedAssetDataService.queryDataList(hgQueryDto);
                fixedDataList.sort(Comparator.comparingDouble(HgFixedAssetData::getGrowthRate));
                for (int i = 0; i < 3; i++) {
                    HgFixedAssetData newFixedData = fixedDataList.get(i);
                    String area = newFixedData.getArea();
                    if(StrUtil.isBlank(area)){
                        continue;
                    }
                    List<HgUser> hgUsers = hgUserMapper.selectUserByArea(area);
                    if (hgUsers != null && !hgUsers.isEmpty()) {
                        HgUser hgUser = hgUsers.get(0);

                        HgReminderData hgReminderDataNew = new HgReminderData();
                        Date date = new Date();
                        hgReminderDataNew.setIHgUser(hgUser.getHuangGangId());
                        hgReminderDataNew.setReminderPerson(hgUser.getRealName());
                        hgReminderDataNew.setCreatedTime(date);
                        hgReminderDataNew.setReminderDepart(hgUser.getDepartment());
                        hgReminderDataNew.setReminderPhoneNumber(hgUser.getPhone());
                        hgReminderDataNew.setReminderTime(date);
                        hgReminderDataNew.setReminderType(4);
                        hgReminderDataNew.setReminderContent(catalogName + "固定资产增速垫底，请注意！");
                        hgReminderDataNew.setReminderTitle(catalogName + "固定资产增速垫底，请注意！");
                        hgReminderDataService.save(hgReminderDataNew);
                        HuangGangUtil.sendMsg(hgReminderDataNew);
                    }
                }
                break;
            case 3:
                hgQueryDto.setAreaLevel(2);
                List<HgFactoryData> factoryList = hgFactoryDataService.queryDataList(hgQueryDto);
                factoryList.sort(Comparator.comparingDouble(HgFactoryData::getGrowthRate));
                for (int i = 0; i < 3; i++) {
                    HgFactoryData hgFactoryData = factoryList.get(i);
                    String area = hgFactoryData.getArea();
                    if(StrUtil.isBlank(area)){
                        continue;
                    }
                    List<HgUser> hgUsers = hgUserMapper.selectUserByArea(area);
                    if (hgUsers != null && !hgUsers.isEmpty()) {
                        HgUser hgUser = hgUsers.get(0);

                        HgReminderData hgReminderDataNew = new HgReminderData();
                        Date date = new Date();
                        hgReminderDataNew.setIHgUser(hgUser.getHuangGangId());
                        hgReminderDataNew.setReminderPerson(hgUser.getRealName());
                        hgReminderDataNew.setCreatedTime(date);
                        hgReminderDataNew.setReminderDepart(hgUser.getDepartment());
                        hgReminderDataNew.setReminderPhoneNumber(hgUser.getPhone());
                        hgReminderDataNew.setReminderTime(date);
                        hgReminderDataNew.setReminderType(4);
                        hgReminderDataNew.setReminderContent(catalogName + "规上工业增速垫底，请注意！");
                        hgReminderDataNew.setReminderTitle(catalogName + "规上工业增速垫底，请注意！");
                        hgReminderDataService.save(hgReminderDataNew);
                        HuangGangUtil.sendMsg(hgReminderDataNew);
                    }
                }
                break;
            case 4:
                hgQueryDto.setAreaLevel(2);
                List<HgConsumptionData> hgConsumptionDataList = hgConsumptionDataService.queryDataList(hgQueryDto);
                hgConsumptionDataList.sort(Comparator.comparingDouble(HgConsumptionData::getGrowthRate));
                for (int i = 0; i < 3; i++) {
                    HgConsumptionData hgConsumptionData = hgConsumptionDataList.get(i);
                    String area = hgConsumptionData.getArea();
                    if(StrUtil.isBlank(area)){
                        continue;
                    }
                    List<HgUser> hgUsers = hgUserMapper.selectUserByArea(area);
                    if (hgUsers != null && !hgUsers.isEmpty()) {
                        HgUser hgUser = hgUsers.get(0);

                        HgReminderData hgReminderDataNew = new HgReminderData();
                        Date date = new Date();
                        hgReminderDataNew.setIHgUser(hgUser.getHuangGangId());
                        hgReminderDataNew.setReminderPerson(hgUser.getRealName());
                        hgReminderDataNew.setCreatedTime(date);
                        hgReminderDataNew.setReminderDepart(hgUser.getDepartment());
                        hgReminderDataNew.setReminderPhoneNumber(hgUser.getPhone());
                        hgReminderDataNew.setReminderTime(date);
                        hgReminderDataNew.setReminderType(4);
                        hgReminderDataNew.setReminderContent(catalogName + "社会消费品零售增速垫底，请注意！");
                        hgReminderDataNew.setReminderTitle(catalogName + "社会消费品零售增速垫底，请注意！");
                        hgReminderDataService.save(hgReminderDataNew);
                        HuangGangUtil.sendMsg(hgReminderDataNew);
                    }
                }
                break;
            case 5:
                List<HgInvestmentData> hgInvestmentDataList = hgInvestmentDataService.queryDataList(hgQueryDto);
                //县市区新签约项目个数
                List<HgInvestmentData> investmentDataListByYoyGrowth = hgInvestmentDataList.stream()
                        .sorted(Comparator.comparingDouble(HgInvestmentData::getYoyGrowth))
                        .collect(Collectors.toList());
                for (int i = 0; i < 3; i++) {
                    HgInvestmentData hgInvestmentData = investmentDataListByYoyGrowth.get(i);
                    String area = hgInvestmentData.getArea();
                    if(StrUtil.isBlank(area)){
                        continue;
                    }
                    List<HgUser> hgUsers = hgUserMapper.selectUserByArea(area);
                    if (hgUsers != null && !hgUsers.isEmpty()) {
                        HgUser hgUser = hgUsers.get(0);

                        HgReminderData hgReminderDataNew = new HgReminderData();
                        Date date = new Date();
                        hgReminderDataNew.setIHgUser(hgUser.getHuangGangId());
                        hgReminderDataNew.setReminderPerson(hgUser.getRealName());
                        hgReminderDataNew.setCreatedTime(date);
                        hgReminderDataNew.setReminderDepart(hgUser.getDepartment());
                        hgReminderDataNew.setReminderPhoneNumber(hgUser.getPhone());
                        hgReminderDataNew.setReminderTime(date);
                        hgReminderDataNew.setReminderType(4);
                        hgReminderDataNew.setReminderContent(catalogName + "新签约项目增速垫底，请注意！");
                        hgReminderDataNew.setReminderTitle(catalogName + "新签约项目增速垫底，请注意！");
                        hgReminderDataService.save(hgReminderDataNew);
                        HuangGangUtil.sendMsg(hgReminderDataNew);
                    }
                }
                //县市区新签约项目协议投资额
                List<HgInvestmentData> investmentDataListByInvYoyGrowth = hgInvestmentDataList.stream()
                        .sorted(Comparator.comparingDouble(HgInvestmentData::getInvYoyGrowth))
                        .collect(Collectors.toList());
                for (int i = 0; i < 3; i++) {
                    HgInvestmentData hgInvestmentData = investmentDataListByInvYoyGrowth.get(i);
                    String area = hgInvestmentData.getArea();
                    if(StrUtil.isBlank(area)){
                        continue;
                    }
                    List<HgUser> hgUsers = hgUserMapper.selectUserByArea(area);
                    if (hgUsers != null && !hgUsers.isEmpty()) {
                        HgUser hgUser = hgUsers.get(0);

                        HgReminderData hgReminderDataNew = new HgReminderData();
                        Date date = new Date();
                        hgReminderDataNew.setIHgUser(hgUser.getHuangGangId());
                        hgReminderDataNew.setReminderPerson(hgUser.getRealName());
                        hgReminderDataNew.setCreatedTime(date);
                        hgReminderDataNew.setReminderDepart(hgUser.getDepartment());
                        hgReminderDataNew.setReminderPhoneNumber(hgUser.getPhone());
                        hgReminderDataNew.setReminderTime(date);
                        hgReminderDataNew.setReminderType(4);
                        hgReminderDataNew.setReminderContent(catalogName + "新签约项目协议投资额增速垫底，请注意！");
                        hgReminderDataNew.setReminderTitle(catalogName + "新签约项目协议投资额增速垫底，请注意！");
                        hgReminderDataService.save(hgReminderDataNew);
                        HuangGangUtil.sendMsg(hgReminderDataNew);
                    }
                }
                //新开工入库项目个数
                List<HgInvestmentData> investmentDataListByInvestedProjYoyGrowth = hgInvestmentDataList.stream()
                        .sorted(Comparator.comparingDouble(HgInvestmentData::getInvestedProjYoyGrowth))
                        .collect(Collectors.toList());
                for (int i = 0; i < 3; i++) {
                    HgInvestmentData hgInvestmentData = investmentDataListByInvestedProjYoyGrowth.get(i);
                    String area = hgInvestmentData.getArea();
                    if(StrUtil.isBlank(area)){
                        continue;
                    }
                    List<HgUser> hgUsers = hgUserMapper.selectUserByArea(area);
                    if (hgUsers != null && !hgUsers.isEmpty()) {
                        HgUser hgUser = hgUsers.get(0);

                        HgReminderData hgReminderDataNew = new HgReminderData();
                        Date date = new Date();
                        hgReminderDataNew.setIHgUser(hgUser.getHuangGangId());
                        hgReminderDataNew.setReminderPerson(hgUser.getRealName());
                        hgReminderDataNew.setCreatedTime(date);
                        hgReminderDataNew.setReminderDepart(hgUser.getDepartment());
                        hgReminderDataNew.setReminderPhoneNumber(hgUser.getPhone());
                        hgReminderDataNew.setReminderTime(date);
                        hgReminderDataNew.setReminderType(4);
                        hgReminderDataNew.setReminderContent(catalogName + "新开工入库项目增速垫底，请注意！");
                        hgReminderDataNew.setReminderTitle(catalogName + "新开工入库项目增速垫底，请注意！");
                        hgReminderDataService.save(hgReminderDataNew);
                        HuangGangUtil.sendMsg(hgReminderDataNew);
                    }
                }
                //招商引资在库项目本年完成投资额
                List<HgInvestmentData> investmentDataListByStorageProjYoyGrowth = hgInvestmentDataList.stream()
                        .sorted(Comparator.comparingDouble(HgInvestmentData::getStorageProjYoyGrowth))
                        .collect(Collectors.toList());
                for (int i = 0; i < 3; i++) {
                    HgInvestmentData hgInvestmentData = investmentDataListByStorageProjYoyGrowth.get(i);
                    String area = hgInvestmentData.getArea();
                    if(StrUtil.isBlank(area)){
                        continue;
                    }
                    List<HgUser> hgUsers = hgUserMapper.selectUserByArea(area);
                    if (hgUsers != null && !hgUsers.isEmpty()) {
                        HgUser hgUser = hgUsers.get(0);

                        HgReminderData hgReminderDataNew = new HgReminderData();
                        Date date = new Date();
                        hgReminderDataNew.setIHgUser(hgUser.getHuangGangId());
                        hgReminderDataNew.setReminderPerson(hgUser.getRealName());
                        hgReminderDataNew.setCreatedTime(date);
                        hgReminderDataNew.setReminderDepart(hgUser.getDepartment());
                        hgReminderDataNew.setReminderPhoneNumber(hgUser.getPhone());
                        hgReminderDataNew.setReminderTime(date);
                        hgReminderDataNew.setReminderType(4);
                        hgReminderDataNew.setReminderContent(catalogName + "招商引资在库项目本年完成投资额增速垫底，请注意！");
                        hgReminderDataNew.setReminderTitle(catalogName + "招商引资在库项目本年完成投资额增速垫底，请注意！");
                        hgReminderDataService.save(hgReminderDataNew);
                        HuangGangUtil.sendMsg(hgReminderDataNew);
                    }
                }
                break;
        }
    }

    /**
     * 用电量最少三名
     */
    private void checkIsUploadByPowerCount3(int catalog, String catalogName) {
        if (catalog == 0) {
            return;
        }
        HgQueryDto hgQueryDto = new HgQueryDto();
        hgQueryDto.setPageSize(1000);
        hgQueryDto.setAreaLevel(2);

        List<HgIndustryElectricityData> hgIndustryElectricityDataList = hgIndustryElectricityDataMapper.queryListWithPage(hgQueryDto);
        hgIndustryElectricityDataList.sort(Comparator.comparingDouble(HgIndustryElectricityData::getPowerCount));
        for (int i = 0; i < 3; i++) {
            HgIndustryElectricityData hgIndustryElectricityData = hgIndustryElectricityDataList.get(i);
            String area = hgIndustryElectricityData.getArea();
            if(StrUtil.isBlank(area)){
                continue;
            }
            List<HgUser> hgUsers = hgUserMapper.selectUserByArea(area);
            if (hgUsers != null && !hgUsers.isEmpty()) {
                HgUser hgUser = hgUsers.get(0);

                HgReminderData hgReminderDataNew = new HgReminderData();
                Date date = new Date();
                hgReminderDataNew.setIHgUser(hgUser.getHuangGangId());
                hgReminderDataNew.setReminderPerson(hgUser.getRealName());
                hgReminderDataNew.setCreatedTime(date);
                hgReminderDataNew.setReminderDepart(hgUser.getDepartment());
                hgReminderDataNew.setReminderPhoneNumber(hgUser.getPhone());
                hgReminderDataNew.setReminderTime(date);
                hgReminderDataNew.setReminderType(4);
                hgReminderDataNew.setReminderContent(catalogName + "工业用电量垫底，请注意！");
                hgReminderDataNew.setReminderTitle(catalogName + "工业用电量垫底，请注意！");
                hgReminderDataService.save(hgReminderDataNew);
                HuangGangUtil.sendMsg(hgReminderDataNew);
            }
        }
    }

    /**
     * 工业产值增长最后3位
     */
    private void checkIsUploadByFactoryGrowthRate3(int catalog, String catalogName) {
        if (catalog == 0) {
            return;
        }
        HgQueryDto hgQueryDto = new HgQueryDto();
        hgQueryDto.setPageSize(1000);
        hgQueryDto.setAreaLevel(2);

        List<HgFactoryPredictionData> hgFactoryPredictionDataList = hgFactoryPredictionDataMapper.queryListWithPage(hgQueryDto);
        hgFactoryPredictionDataList.sort(Comparator.comparingDouble(HgFactoryPredictionData::getGrowthRate));
        for (int i = 0; i < 3; i++) {
            HgFactoryPredictionData hgFactoryPredictionData = hgFactoryPredictionDataList.get(i);
            String area = hgFactoryPredictionData.getArea();
            if(StrUtil.isBlank(area)){
                continue;
            }
            List<HgUser> hgUsers = hgUserMapper.selectUserByArea(area);
            if (hgUsers != null && !hgUsers.isEmpty()) {
                HgUser hgUser = hgUsers.get(0);

                HgReminderData hgReminderDataNew = new HgReminderData();
                Date date = new Date();
                hgReminderDataNew.setIHgUser(hgUser.getHuangGangId());
                hgReminderDataNew.setReminderPerson(hgUser.getRealName());
                hgReminderDataNew.setCreatedTime(date);
                hgReminderDataNew.setReminderDepart(hgUser.getDepartment());
                hgReminderDataNew.setReminderPhoneNumber(hgUser.getPhone());
                hgReminderDataNew.setReminderTime(date);
                hgReminderDataNew.setReminderType(4);
                hgReminderDataNew.setReminderContent(catalogName + "工业产值增长垫底，请注意！");
                hgReminderDataNew.setReminderTitle(catalogName + "工业产值增长垫底，请注意！");
                hgReminderDataService.save(hgReminderDataNew);
                HuangGangUtil.sendMsg(hgReminderDataNew);
            }
        }
    }

    /**
     * 招商最后3名
     */
    private void checkIsUploadByInvestmentLast3(int catalog, String catalogName) {
        if (catalog == 0) {
            return;
        }
        HgQueryDto hgQueryDto = new HgQueryDto();
        hgQueryDto.setPageSize(1000);

        List<HgInvestmentPartData> hgInvestmentPartDataList = hgInvestmentPartDataService.queryDataList(hgQueryDto);

        //在谈项目
        List<HgInvestmentPartData> investmentDataListByNegotiatingBusinessCumNum = hgInvestmentPartDataList.stream()
                .sorted(Comparator.comparingInt(HgInvestmentPartData::getNegotiatingBusinessCumNum))
                .collect(Collectors.toList());
        for (int i = 0; i < 3; i++) {
            HgInvestmentPartData hgInvestmentPartData = investmentDataListByNegotiatingBusinessCumNum.get(i);
            String area = hgInvestmentPartData.getArea();
            if (StrUtil.isBlank(area)) {
                continue;
            }
            List<HgUser> hgUsers = hgUserMapper.selectUserByArea(area);
            if (hgUsers != null && !hgUsers.isEmpty()) {
                HgUser hgUser = hgUsers.get(0);

                HgReminderData hgReminderDataNew = new HgReminderData();
                Date date = new Date();
                hgReminderDataNew.setIHgUser(hgUser.getHuangGangId());
                hgReminderDataNew.setReminderPerson(hgUser.getRealName());
                hgReminderDataNew.setCreatedTime(date);
                hgReminderDataNew.setReminderDepart(hgUser.getDepartment());
                hgReminderDataNew.setReminderPhoneNumber(hgUser.getPhone());
                hgReminderDataNew.setReminderTime(date);
                hgReminderDataNew.setReminderType(4);
                hgReminderDataNew.setReminderContent(catalogName + "专班在谈项目偏少，请加强项目招引工作");
                hgReminderDataNew.setReminderTitle(catalogName + "专班在谈项目偏少，请加强项目招引工作");
                hgReminderDataService.save(hgReminderDataNew);
                HuangGangUtil.sendMsg(hgReminderDataNew);
            }
        }

        //签约项目
        List<HgInvestmentPartData> investmentDataListBySignedProjectCumNum = hgInvestmentPartDataList.stream()
                .sorted(Comparator.comparingInt(HgInvestmentPartData::getSignedProjectCumNum))
                .collect(Collectors.toList());
        for (int i = 0; i < 3; i++) {
            HgInvestmentPartData hgInvestmentPartData = investmentDataListBySignedProjectCumNum.get(i);
            String area = hgInvestmentPartData.getArea();
            if (StrUtil.isBlank(area)) {
                continue;
            }
            List<HgUser> hgUsers = hgUserMapper.selectUserByArea(area);
            if (hgUsers != null && !hgUsers.isEmpty()) {
                HgUser hgUser = hgUsers.get(0);

                HgReminderData hgReminderDataNew = new HgReminderData();
                Date date = new Date();
                hgReminderDataNew.setIHgUser(hgUser.getHuangGangId());
                hgReminderDataNew.setReminderPerson(hgUser.getRealName());
                hgReminderDataNew.setCreatedTime(date);
                hgReminderDataNew.setReminderDepart(hgUser.getDepartment());
                hgReminderDataNew.setReminderPhoneNumber(hgUser.getPhone());
                hgReminderDataNew.setReminderTime(date);
                hgReminderDataNew.setReminderType(4);
                hgReminderDataNew.setReminderContent(catalogName + "专班签约项目偏少，请加强项目招引工作");
                hgReminderDataNew.setReminderTitle(catalogName + "专班签约项目偏少，请加强项目招引工作");
                hgReminderDataService.save(hgReminderDataNew);
                HuangGangUtil.sendMsg(hgReminderDataNew);
            }
        }

        //协议投资额项目
        List<HgInvestmentPartData> investmentDataListByAgreedInvestmentCumAmt = hgInvestmentPartDataList.stream()
                .sorted(Comparator.comparingDouble(HgInvestmentPartData::getAgreedInvestmentCumAmt))
                .collect(Collectors.toList());
        for (int i = 0; i < 3; i++) {
            HgInvestmentPartData hgInvestmentPartData = investmentDataListByAgreedInvestmentCumAmt.get(i);
            String area = hgInvestmentPartData.getArea();
            if (StrUtil.isBlank(area)) {
                continue;
            }
            List<HgUser> hgUsers = hgUserMapper.selectUserByArea(area);
            if (hgUsers != null && !hgUsers.isEmpty()) {
                HgUser hgUser = hgUsers.get(0);

                HgReminderData hgReminderDataNew = new HgReminderData();
                Date date = new Date();
                hgReminderDataNew.setIHgUser(hgUser.getHuangGangId());
                hgReminderDataNew.setReminderPerson(hgUser.getRealName());
                hgReminderDataNew.setCreatedTime(date);
                hgReminderDataNew.setReminderDepart(hgUser.getDepartment());
                hgReminderDataNew.setReminderPhoneNumber(hgUser.getPhone());
                hgReminderDataNew.setReminderTime(date);
                hgReminderDataNew.setReminderType(4);
                hgReminderDataNew.setReminderContent(catalogName + "专班协议投资额偏少，请加强项目招引工作");
                hgReminderDataNew.setReminderTitle(catalogName + "专班协议投资额偏少，请加强项目招引工作");
                hgReminderDataService.save(hgReminderDataNew);
                HuangGangUtil.sendMsg(hgReminderDataNew);
            }
        }

    }


    /**
     * 数据是否是本月提交的
     * @param date
     * @return
     */
    public static boolean isDateInCurrentMonth(Date date) {
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate now = LocalDate.now();
        return localDate.getMonth() == now.getMonth() && localDate.getYear() == now.getYear();
    }

}
